﻿using MauiTurntable.dbModel;
using Microsoft.Maui.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Runtime.InteropServices.JavaScript.JSType;
using Font = Microsoft.Maui.Graphics.Font;

namespace MauiTurntable.ViewModel
{
    /// <summary>
    /// 
    /// </summary>
    public class GraphicsDrawable : IDrawable
    {
        ICanvas canvas;
        public RectF dirtyRect;
        public event Action<RectF> outdata;
        List<Color> _colors { get; set; }
        float x { get; set; }
        float y { get; set; }
        float x1 { get; set; }
        float y1 { get; set; }
        float size { get; set; }
        int colorIndex { get; set; }
        int fontsize { get; set; }  
        /// <summary>
        /// 
        /// </summary>
        public List<GraphicsDrawableItem> graphicsDrawableItems { get; set; }
        public GraphicsDrawable()
        {
            graphicsDrawableItems = new List<GraphicsDrawableItem>();
            _colors = new List<Color>();
            _colors.Add(Colors.Teal);
            _colors.Add(Colors.Green);
            _colors.Add(Colors.Orange);
            dirtyRect = new RectF();
            fontsize = 17;
        }
        public void Draw(ICanvas canvas, RectF dirtyRect)
        {
            this.canvas = canvas;
            this.dirtyRect = dirtyRect;
            // canvas.DrawString($"{dirtyRect}",10,10,horizontalAlignment:HorizontalAlignment.Center);
            //outdata?.Invoke(dirtyRect); 
            if (this.dirtyRect.Width <= 0 || this.dirtyRect.Height <= 0)
            {
                return;
            }
            Rraw();

            //canvas.FillRectangle(dirtyRect);
        }
        public void Rraw()
        {
            colorIndex = 0;
            //   canvas.Rotate(11,dirtyRect.Center.X,dirtyRect.Center.Y);
            size = dirtyRect.Width;
            if (size > dirtyRect.Height)
            {
                size = dirtyRect.Height;
            }
            x = dirtyRect.Center.X - size / 2;
            y = dirtyRect.Center.Y - size / 2;
            x1 = dirtyRect.Center.X + size / 2;
            y1 = dirtyRect.Center.Y + size / 2;
            foreach (var item in graphicsDrawableItems)
            {
                DrawShanXing(item);
            }
            canvas.FontSize = fontsize;
            Font font = new Microsoft.Maui.Graphics.Font("微软雅黑");
            canvas.Font = font;
            foreach (var item in graphicsDrawableItems)
            {
                DrawString(item, font);
            }

        }
        private void DrawShanXing(GraphicsDrawableItem data)
        {
            if (colorIndex >= _colors.Count)
                colorIndex = 0;
            try
            {
                //canvas.Rotate(0);
                PathF path = new PathF();
                path.AddArc(x, y, x1, y1, data.startAngle, data.endAngle, false);
                path.LineTo(dirtyRect.Center.X, dirtyRect.Center.Y);

                path.Close();
                canvas.FillColor = _colors[colorIndex++];
                canvas.FillPath(path);
            }
            catch (Exception ex) { }

        }
        private void DrawString(GraphicsDrawableItem data, Font font)
        {
            try
            {
                canvas.SaveState();
                canvas.Rotate(data.tetxAngle, dirtyRect.Center.X, dirtyRect.Center.Y);
                string drawStr = data.drawText;
                var fise = canvas.GetStringSize(drawStr, font, fontsize);
                float startx = 0;
                if (fise.Width> size / 2)
                {
                    startx = dirtyRect.Center.X;
                }
                else
                {
                    startx = dirtyRect.Center.X + (size / 2 - fise.Width) / 2;
                }
                canvas.DrawString(data.drawText, startx, dirtyRect.Center.Y, HorizontalAlignment.Left);
                //canvas.DrawString($"w:{dirtyRect.Width} h:{dirtyRect.Height} cx:{dirtyRect.Center} size:{size}", dirtyRect.Center.X + size / 4, dirtyRect.Center.Y, HorizontalAlignment.Center);
                canvas.RestoreState();
            }
            catch (Exception ex) { }

        }
        public void Sub(List<DBItem> data)
        {
            float max = 0;
            foreach (DBItem item in data)
            {
                max += item.value;
            }
            float maxItem = 360 / max;
            graphicsDrawableItems.Clear();

            float startAngle = 0;
            for (int i = 0; i < data.Count; i++)
            {
                GraphicsDrawableItem gi = new GraphicsDrawableItem()
                {
                    drawText = data[i].name,
                    startAngle = startAngle,
                    endAngle = startAngle + maxItem * data[i].value
                };
                if (i == data.Count - 1)
                {
                    gi.endAngle = 360;
                }
                startAngle = gi.endAngle;
                graphicsDrawableItems.Add(gi);
            }
            //Rraw();
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public class GraphicsDrawableItem
    {
        /// <summary>
        /// 开始角度
        /// </summary>
        public float startAngle { get; set; }
        /// <summary>
        /// 结束角度
        /// </summary>
        public float endAngle { get; set; }
        /// <summary>
        /// 绘制的文本
        /// </summary>
        public string drawText { get; set; }
        /// <summary>
        /// 文本角度
        /// </summary>
        public float tetxAngle
        {
            get
            {
                var dst = endAngle - ((endAngle - startAngle) / 2);
                return 0 - dst;
            }
        }
    }
}
